package com.westar.core.job;

import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.TriggerKey;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 定时任务封装类
 */
@Component
public class DynamicJobFactory {
 
    @Resource
    private Scheduler scheduler;

    /**
     * 添加job
     * @param clazz 执行该任务的class
     * @param jobDataMap 封装的参数
     * @param affairId 办件ID
     * @param cron 定时执行时间
     * @throws Exception
     */
    public void addJob(Class clazz, JobDataMap jobDataMap, String affairId, String cron) throws Exception {


        JobDetail jobDetail = JobBuilder.newJob(clazz)
                .withIdentity("jobName:" + affairId, Scheduler.DEFAULT_GROUP)
                //如果需要给任务传递参数，可以用setJobData来设置参数
                .setJobData(jobDataMap)
                .build();
        CronTriggerImpl cronTrigger = new CronTriggerImpl();
        cronTrigger.setName("jobTrigger:" + affairId);
        cronTrigger.setCronExpression(cron);

        JobKey jobKey = new JobKey("jobName:" + affairId, Scheduler.DEFAULT_GROUP);
        if (!scheduler.checkExists(jobKey)) {
            scheduler.scheduleJob(jobDetail, cronTrigger);
        }
    }

    /**
     * 暂停job
     * @param affairId 办件ID
     * @throws Exception
     */
    public void pauseJob(String affairId) throws Exception {
        JobKey jobKey = new JobKey("jobName:" + affairId, Scheduler.DEFAULT_GROUP);
        scheduler.pauseJob(jobKey);
    }
 
    /**
     * 重启job
     * @param affairId 办件ID
     * @throws Exception
     */
    public void resumeJob(String affairId) throws Exception {
        JobKey jobKey = new JobKey("jobName:" + affairId, Scheduler.DEFAULT_GROUP);
        scheduler.resumeJob(jobKey);
    }
 
    /**
     * 停止job
     * @param affairId 办件ID
     * @throws Exception
     */
    public void stopJob(String affairId) throws Exception {
        JobKey jobKey = new JobKey("jobName:" + affairId, Scheduler.DEFAULT_GROUP);
        scheduler.deleteJob(jobKey);
    }
 
    /**
     * 修改job的执行时间
     * @param affairId 办件ID
     * @param cronExpression 新的时间
     * @throws Exception
     */
    public void updateJobTime(String affairId, String cronExpression) throws Exception {
        TriggerKey triggerKey = new TriggerKey("jobTrigger:" + affairId, Scheduler.DEFAULT_GROUP);
        CronTriggerImpl trigger = (CronTriggerImpl) scheduler.getTrigger(triggerKey);
        if (trigger == null) {
            return;
        }
        String oldTime = trigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(cronExpression)) {
            trigger.setCronExpression(cronExpression);
            scheduler.rescheduleJob(triggerKey, trigger);
        }
    }
 
    /**
     * 获取job信息
     * @param affairId 办件ID
     * @throws Exception
     */
    public JobDetail getJobDetail(String affairId) throws Exception {
        JobKey jobKey = new JobKey("jobName:" + affairId, Scheduler.DEFAULT_GROUP);
        JobDetail detail = scheduler.getJobDetail(jobKey);
        return detail;
    }
 
    /**
     * 启动所有的任务
     * @throws Exception
     */
    public void startJobs() throws Exception {
        scheduler.start();
    }
 
    /**
     * shutdown所有的任务
     * @throws Exception
     */
    public void shutdownJobs() throws Exception {
        if (!scheduler.isShutdown()) {
            scheduler.shutdown();
        }
    }
}